package com.example.shiwuyouproject.ui.fragment.my.activity

import android.graphics.Color
import android.view.View
import android.widget.TextView
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.bigkoo.pickerview.view.TimePickerView
import com.example.shiwuyouproject.R
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityTeachingexperienceBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.TeachingExperienceViewModel
import com.example.shiwuyouproject.ui.my.bean.PersonalInformationBean
import com.example.shiwuyouproject.utils.MyTimeUtils
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import java.util.*

/**
 * 教学经历
 */
class TeachingExperienceActivity:BaseVmActivity<ActivityTeachingexperienceBinding, TeachingExperienceViewModel>(){
    override fun viewModelClass(): Class<TeachingExperienceViewModel> =TeachingExperienceViewModel::class.java

    override fun getViewBinding(): ActivityTeachingexperienceBinding {
    return ActivityTeachingexperienceBinding.inflate(layoutInflater)
    }

    override fun initView() {
        initdegreeStatusPicker()
        initCustomTimePicker()
        mViewModel.getRealNameAuthenticationData()

    }

    override fun initData() {

    }
    private var  teachschool:String=""
    private var  teach_desc:String=""
    override fun setListener() {
        mBinding.tvBack.setOnClickListener { finish() }
      mBinding.tvTeachschool.setOnClickListener {
          pvdegreeStatus?.show()
      }

     mBinding.tvTeachstartTime.setOnClickListener { //开始时间
         timeStatus=true
         pvCustomLunar?.show()
     }
        mBinding.tvteachendTime.setOnClickListener { //结束时间
            timeStatus=false
            pvCustomLunar?.show()
        }


        mBinding.btSava.setOnClickListener {
            YUtils.showLoading(this, "保存中...")
            teach_desc=mBinding.etteachDesc.text.toString()
            mViewModel.preservationCard(teachschool,myStStartTime,myStEndTime,teach_desc)
        }
    }

    override fun observe() {
        mViewModel.getGenRenStatus.observe(this,androidx.lifecycle.Observer<PersonalInformationBean>{
            when(it.teachschool){
                1 ->{
                    mBinding.tvTeachschool.text = "其他"
                }
                2 ->{
                    mBinding.tvTeachschool.text = "公办学校"
                }
                3 ->{
                    mBinding.tvTeachschool.text = "民办学校"
                }
                4 ->{
                    mBinding.tvTeachschool.text = "国际机构"
                }
                5 ->{
                    mBinding.tvTeachschool.text = "机构"
                }
            }
            teachschool=it.teachschool.toString()
            if(it.teachstart_time != "0") {
                myStStartTime=it.teachstart_time
                mBinding.tvTeachstartTime.text = it.teachstart_time
            }
            if(it.teachend_time != "0") {
                mBinding.tvteachendTime.text = it.teachend_time
                myStEndTime=it.teachend_time
            }
            mBinding.etteachDesc.setText(it.teach_desc)
            teach_desc=it.teach_desc
        })
        mViewModel.getPostStatus.observe(this,androidx.lifecycle.Observer<Boolean>{
            YUtils.hideLoading()
            ToastUtil.show("保存成功！")
            finish()
        })

    }

    /**
     * 教学单位
     */
    private var pvdegreeStatus: OptionsPickerView<*>? = null
    private fun initdegreeStatusPicker() {
        val workData:MutableList<String> = ArrayList()
        workData.add("其他")
        workData.add("公办学校")
        workData.add("民办学校")
        workData.add("国际机构")
        workData.add("机构")
        pvdegreeStatus = OptionsPickerBuilder(this) { options1, _, _, v -> //返回的分别是三个级别的选中位置
            mBinding.tvTeachschool.text= workData[options1]
            teachschool=(options1+1).toString()
        }
            .setTitleText("教学单位")
            .setContentTextSize(20) //设置滚轮文字大小
            .setDividerColor(Color.LTGRAY) //设置分割线的颜色
            .setSelectOptions(0, 1) //默认选中项
            .isRestoreItem(true) //切换时是否还原，设置默认选中第一项。
            .setOutSideColor(0x00000000) //设置外部遮罩颜色
            .build<Any>()
        (pvdegreeStatus as OptionsPickerView<Any>?)?.setPicker(workData as List<Any>?) //二级选择器
    }

    private var timeStatus=false
    var pvCustomLunar: TimePickerView? = null
    var cuurDataTime: Date?=null
    var stStartTime: Date?=null
    var stendTime: Date?=null
    var myStStartTime=""
    var myStEndTime=""
    private fun initCustomTimePicker() {
        val selectedDate = Calendar.getInstance() //系统当前时间
        val startDate = Calendar.getInstance()
        startDate[1990, 1] = 23
        val endDate = Calendar.getInstance()
        endDate[2069, 2] = 28
        //时间选择器 ，自定义布局
        //时间选择器 ，自定义布局
        pvCustomLunar = TimePickerBuilder(
            this
        ) { date, v -> //选中事件回调
            cuurDataTime=date
        }
            .setDate(selectedDate)
            .setRangDate(startDate, endDate)
            .setLayoutRes(
                R.layout.pickerview_custom_lunar
            ) { v ->
                val tvSubmit = v.findViewById<View>(R.id.tv_finish) as TextView
                val ivCancel = v.findViewById<View>(R.id.iv_cancel) as TextView
                tvSubmit.setOnClickListener {
                    pvCustomLunar?.returnData()
                    pvCustomLunar?.dismiss()
                    if(startDate!=null&&cuurDataTime!=null){

                    }
                    if(timeStatus){
                        stStartTime=cuurDataTime
                        myStStartTime= stStartTime?.let { it1 -> MyTimeUtils.getTimes(it1) }!!
                        mBinding.tvTeachstartTime.text= stStartTime?.let { it1 ->
                            MyTimeUtils.getTimesYe(
                                it1
                            )
                        }
                    }else{
                        stendTime=cuurDataTime
                        myStEndTime= stendTime?.let { it1 -> MyTimeUtils.getTimes(it1) }!!
                        mBinding.tvteachendTime.text= stendTime?.let { it1 ->
                            MyTimeUtils.getTimesYe(
                                it1
                            )
                        }
                    }
                }
                ivCancel.setOnClickListener { pvCustomLunar?.dismiss() }
            }
            .setDividerColor(Color.WHITE)//设置分割线的颜色
            .setType(booleanArrayOf(true, true, false, false, false, false))
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .setDividerColor(Color.RED)
            .build()
    }
}